﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ChiaSeThongTinWebService.Interface;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace ChiaSeThongTinWebService
{
    /// <summary>
    /// Summary description for CSTT_Service
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class CSTT_Service : IFriend, IPost, IComment, ILike, IDislike, ICheDoXem,INotifications
    {
        
        #region private
            private string SetIDFriend()
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    string fr = data.FRIENDs.Last().ID;
                    fr = fr.Replace("FR", string.Empty);
                    long NewID = 1 + long.Parse(fr);
                    return "FR" + NewID.ToString("00000000");
                }
                catch
                {
                    return "FR00000000";
                }
            }
            private string SetIDFriendRequests()
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    string fr = data.FRIENDREQUESTs.Last().ID;
                    fr = fr.Replace("FRR", string.Empty);
                    long NewID = 1 + long.Parse(fr);
                    return "FRR" + NewID.ToString("00000000");
                }
                catch
                {
                    return "FRR00000000";
                }
            }
            private string SetIDPost()
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    string fr = data.POSTs.Last().ID_POST;
                    fr = fr.Replace("P", string.Empty);
                    long NewID = 1 + long.Parse(fr);
                    return "P" + NewID.ToString("00000000");
                }
                catch
                {
                    return "P00000000";
                }
            }
            private string SetIDComment()
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    string fr = data.COMMENTs.Last().ID_COMMENT;
                    fr = fr.Replace("CMT", string.Empty);
                    long NewID = 1 + long.Parse(fr);
                    return "CMT" + NewID.ToString("00000000");
                }
                catch
                {
                    return "CMT00000000";
                }
            }
            private string SetIDFollwingPost()
        {
            try
            {
                var data = new Data_ChiaSeThongTinDataContext();
                string fr = data.FOLLOWINGPOSTs.Last().ID;
                fr = fr.Replace("FLP", string.Empty);
                long NewID = 1 + long.Parse(fr);
                return "FLP" + NewID.ToString("00000000");
            }
            catch
            {
                return "FLP00000000";
            }
        }
            private string SetIDNotifications()
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    string fr = data.NOTIFICATIONs.Last().ID;
                    fr = fr.Replace("NTF", string.Empty);
                    long NewID = 1 + long.Parse(fr);
                    return "NTF" + NewID.ToString("00000000");
                }
                catch
                {
                    return "NTF00000000";
                }
            }
        #endregion

        #region Friend

            [WebMethod]
            public string[] GetAllFriends(string ID_Host)
            {
                var data = new Data_ChiaSeThongTinDataContext();            
                var temp = (from inf in data.FRIENDs where (inf.ID_FRIEND1 == ID_Host) select inf.ID_FRIEND2).ToArray();
                return temp;            
            }
        
            [WebMethod]
            public FRIENDREQUEST[] GetAllFriendRequests(string ID_Host)
            {
                var data = new Data_ChiaSeThongTinDataContext();        
                var temp = (from inf in data.FRIENDREQUESTs where inf.ID_HOST == ID_Host select inf).ToArray();
                return temp;            
            }      
        
            [WebMethod]
            public bool AddFriend(string ID,bool OK)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.FRIENDREQUESTs where inf.ID_HOST == ID select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    if (OK)
                    {
                        FRIEND fr = new FRIEND();
                        fr.ID = SetIDFriend();
                        fr.ID_FRIEND1 = temp.ID_HOST;
                        fr.ID_FRIEND2 = temp.ID_FRIEND;
                        data.FRIENDs.InsertOnSubmit(fr);
                        data.SubmitChanges();
                    }
                    data.FRIENDREQUESTs.DeleteOnSubmit(temp);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        
            [WebMethod]
            public bool DeleteFriend(string ID)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.FRIENDs where inf.ID == ID select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    data.FRIENDs.DeleteOnSubmit(temp);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }

            [WebMethod]
            public bool AddFriendRequests(FRIENDREQUEST FrR)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.FRIENDREQUESTs where inf.ID_HOST == FrR.ID_HOST && inf.ID_FRIEND==FrR.ID_FRIEND select inf).FirstOrDefault();
                    if (temp != null)
                        return false;
                    FrR.ID = SetIDFriendRequests();
                    data.FRIENDREQUESTs.InsertOnSubmit(FrR);
                    data.SubmitChanges();                
                    return true;
                }
                catch
                {
                    return false;
                }
            }

        #endregion

        #region Post 

            [WebMethod]
            public POST[] GetPostH(string ID_Host, int stt)
            {
                var data = new Data_ChiaSeThongTinDataContext();               
                var post1 = (from inf in data.POSTs where inf.ID_POSTER==ID_Host orderby inf.NGAYTHANG descending select inf).Skip(10 * stt).Take(10).ToArray();
                return post1;
            }

            [WebMethod]
            public POST[] GetPostF(string ID_Host,int stt)
            {
                var data = new Data_ChiaSeThongTinDataContext();
                var Lfriend = (from inf in data.FRIENDs where inf.ID_FRIEND1 == ID_Host || inf.ID_FRIEND2 == ID_Host select inf).ToList();
                var post1 = (from inf in data.POSTs where (inf.ID_CDX == "1" || inf.ID_CDX == "2") && Lfriend.Exists(element => element.ID_FRIEND2 == inf.ID_POSTER) orderby inf.NGAYTHANG descending select inf).Skip(10 * stt).Take(10).ToArray();//chế dộ?
                return post1;           
            }

            [WebMethod]
            public POST[] GetPostP(string ID_Host, int stt)
            {
                var data = new Data_ChiaSeThongTinDataContext();                
                var post1 = (from inf in data.POSTs where inf.ID_CDX == "1"  orderby inf.NGAYTHANG descending select inf).Skip(10 * stt).Take(10).ToArray();
                return post1;
            }

            [WebMethod]
            public NPost GetPost(string ID_Host)
            {
                var data = new Data_ChiaSeThongTinDataContext();
                NPost np = new NPost();
                np.post = (from inf in data.POSTs where inf.ID_POST== ID_Host select inf).FirstOrDefault();
                np.likes= (from inf in data.LIKE_PCs where inf.ID_PC == ID_Host select inf).Count();
                np.dislikes = (from inf in data.DISLIKE_PCs where inf.ID_PC == ID_Host select inf).Count();
                return np;
            }
            

            [WebMethod]
            public bool AddPost(POST Npost)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.POSTs where inf.ID_POSTER == Npost.ID_POSTER && inf.NGAYTHANG==Npost.NGAYTHANG select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    Npost.ID_POST = SetIDPost();
                    data.POSTs.InsertOnSubmit(Npost);
                    data.SubmitChanges();          
                    return true;
                }
                catch
                {
                    return false;
                }
            }

            [WebMethod]
            public bool UpdatePost(POST Npost)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.POSTs where inf.ID_POST== Npost.ID_POST  select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    temp.ID_CDX = Npost.ID_CDX;
                    temp.NOIDUNG = Npost.NOIDUNG;
                    temp.NGAYTHANG = Npost.NGAYTHANG;
                    temp.TIEUDE = Npost.TIEUDE;
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }

            [WebMethod]
            public bool DeletePost(string ID)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.POSTs where inf.ID_POST == ID select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    var comment = (from inf in data.COMMENTs where inf.ID_POST == temp.ID_POST select inf).ToList();
                    var followingpost = (from inf in data.FOLLOWINGPOSTs where inf.ID_POST == temp.ID_POST select inf).ToList();
                    
                    var likepost = (from inf in data.LIKE_PCs where inf.ID_PC == temp.ID_POST select inf).ToList();
                    var dislikepost = (from inf in data.DISLIKE_PCs where inf.ID_PC == temp.ID_POST select inf).ToList();
                    data.LIKE_PCs.DeleteAllOnSubmit(likepost);
                    data.DISLIKE_PCs.DeleteAllOnSubmit(dislikepost);
                    data.SubmitChanges();
                    data.FOLLOWINGPOSTs.DeleteAllOnSubmit(followingpost);
                    data.SubmitChanges();

                    //foreach (var cmt in comment)
                    //{
                    //    var likepost1 = (from inf in data.LIKE_PCs where inf.ID_PC == cmt.ID_POST select inf).ToList();
                    //    var dislikepost1 = (from inf in data.DISLIKE_PCs where inf.ID_PC == cmt.ID_POST select inf).ToList();
                    //    data.LIKE_PCs.DeleteAllOnSubmit(likepost1);
                    //    data.DISLIKE_PCs.DeleteAllOnSubmit(dislikepost1);                        
                    //}
                    //data.SubmitChanges();

                    data.COMMENTs.DeleteAllOnSubmit(comment);
                    data.SubmitChanges();
                    data.POSTs.DeleteOnSubmit(temp);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }

            [WebMethod]
            public bool UnfollowingPost(string ID_Post, string ID_follower)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.FOLLOWINGPOSTs where inf.ID_POST == ID_Post && inf.ID_FOLLOWER == ID_follower select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    data.FOLLOWINGPOSTs.DeleteOnSubmit(temp);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }

        #endregion

        #region Comment
            [WebMethod]
            public NComment[] GetComment(string ID_post)
            {
                var data = new Data_ChiaSeThongTinDataContext();
                var cmt = (from inf in data.COMMENTs where inf.ID_POST == ID_post select inf).ToArray();
                NComment[] LNcmt = new NComment[cmt.Count()];
                for (int i = 0; i < cmt.Count(); ++i)
                {
                    LNcmt[i].comment = cmt[i];
                    LNcmt[i].likes = (from inf in data.LIKE_PCs where inf.ID_PC == cmt[i].ID_COMMENT select inf).Count();
                    LNcmt[i].dislikes = (from inf in data.DISLIKE_PCs where inf.ID_PC == cmt[i].ID_COMMENT select inf).Count();
                }
                return LNcmt;
            }

            [WebMethod]
            public bool AddComment(COMMENT Ncomment)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    Ncomment.ID_COMMENT = SetIDComment();
                    data.COMMENTs.InsertOnSubmit(Ncomment);
                    var follwingpost = (from inf in data.FOLLOWINGPOSTs where inf.ID_POST == Ncomment.ID_POST select inf).ToList();
                    List<NOTIFICATION> Lnotifications = new List<NOTIFICATION>();
                    long NewID;
                    bool newf = false;
                    try
                    {
                        string fr = data.NOTIFICATIONs.Last().ID;

                        NewID = 1 + long.Parse(fr);
                    }
                    catch
                    {
                        NewID = 0;
                    }                   
                    foreach (var fp in follwingpost)
                    {
                        NOTIFICATION nc = new NOTIFICATION();
                        nc.ID = NewID.ToString("00000000");
                        nc.ID_OBJECT = Ncomment.ID_COMMENT;
                        nc.ID_PERSON = fp.ID_FOLLOWER;
                        nc.STYLE = "Comment";
                        Lnotifications.Add(nc);
                        NewID++;
                        if(fp.ID_FOLLOWER==Ncomment.ID_COMMENTATOR)
                            newf=true;
                    }
                    data.NOTIFICATIONs.InsertAllOnSubmit(Lnotifications);
                    if(newf)
                    {
                        FOLLOWINGPOST fp2 = new FOLLOWINGPOST();
                        fp2.ID = SetIDFollwingPost();
                        fp2.ID_POST = Ncomment.ID_POST;
                        fp2.ID_FOLLOWER = Ncomment.ID_COMMENTATOR;
                        data.FOLLOWINGPOSTs.InsertOnSubmit(fp2);
                    }
                    data.SubmitChanges();

                    return true;
                }
                catch
                {
                    return false;
                }
            }

            [WebMethod]
            public bool UpdateComment(COMMENT Ncomment)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.COMMENTs where inf.ID_COMMENT == Ncomment.ID_COMMENT select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    temp.NGAYTHANG = Ncomment.NGAYTHANG;
                    temp.NOIDUNG = Ncomment.NOIDUNG;
                    data.COMMENTs.InsertOnSubmit(Ncomment);               
                    data.SubmitChanges();

                    return true;
                }
                catch
                {
                    return false;
                }
            }

            [WebMethod]
            public bool DeleteComment(string ID)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.COMMENTs where inf.ID_COMMENT == ID select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    var likepost = (from inf in data.LIKE_PCs where inf.ID_PC == temp.ID_COMMENT select inf).ToList();
                    var dislikepost = (from inf in data.DISLIKE_PCs where inf.ID_PC == temp.ID_COMMENT select inf).ToList();
                    data.LIKE_PCs.DeleteAllOnSubmit(likepost);
                    data.DISLIKE_PCs.DeleteAllOnSubmit(dislikepost);
                    data.SubmitChanges();
                    data.COMMENTs.DeleteOnSubmit(temp);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }

            }

            [WebMethod]
            public decimal? NumberOfComment(string ID)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.COMMENTs where inf.ID_POST == ID select inf).ToList();
                    if (temp == null)
                        return 0;
                    return temp.Count;
                }
                catch
                {
                    return 0;
                }
            }
        #endregion

        #region Notification
            [WebMethod]
            public NOTIFICATION[] GetAllNotifications(string ID_Host)
            {
                var data = new Data_ChiaSeThongTinDataContext();
                var notifications = (from inf in data.NOTIFICATIONs where inf.ID_PERSON== ID_Host select inf).ToArray();
                return notifications;
            }

            [WebMethod]
            public bool DeleteNotifications(string ID)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.NOTIFICATIONs where inf.ID == ID select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    data.NOTIFICATIONs.DeleteOnSubmit(temp);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }

           
        #endregion

        #region Like 
           
            [WebMethod]
            public LIKE_PC[] GetLikePC(string ID_post)
            {                    
                var data = new Data_ChiaSeThongTinDataContext();
                var temp = (from inf in data.LIKE_PCs where inf.ID_PC == ID_post select inf).ToArray();
                return temp;  
            }

            [WebMethod]
            public long GetNumberLikePC(string ID_post)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.LIKE_PCs where inf.ID_PC == ID_post select inf);
                    if (temp == null)
                        return 0;
                    return temp.Count();  
                }
                catch
                {
                    return 0;
                }
            }

            [WebMethod]
            public LIKE_PC[] GetMyLikes(string ID_host)
            {
                var data = new Data_ChiaSeThongTinDataContext();
                var temp = (from inf in data.LIKE_PCs where inf.ID_LIKER == ID_host select inf).ToArray();
                return temp;  
            }

            [WebMethod]
            public decimal? GetNumberLikes(string ID_host)
            {      
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.LIKEs where inf.ID_HOST == ID_host select inf.NUMB).FirstOrDefault();
                    if (temp == null)
                        return 0;
                    return temp;  
                }
                catch
                {
                    return 0;
                }
            }

            //[WebMethod]
            //public bool AddLikePost(LIKE_PC likepost)
            //{
            //    try
            //    {
            //        var data = new Data_ChiaSeThongTinDataContext();
            //        string fr = data.LIKE_PCs.Last().ID;
            //        fr = fr.Replace("LP", string.Empty);
            //        long NewID = 1 + long.Parse(fr);
            //        likepost.ID = "LP" + NewID.ToString("00000000");
            //        data.LIKE_PCs.InsertOnSubmit(likepost);
            //        var follwingpost = (from inf in data.FOLLOWINGPOSTs where inf.ID_POST == likepost.ID_PC select inf).ToList();
            //        bool ok = true;
            //        foreach (var fp in follwingpost)
            //        {
            //            if (fp.ID_FOLLOWER == likepost.ID_LIKER)
            //            {
            //                ok = false;
            //                break;
            //            }
            //        }
            //        if (ok)
            //        {
            //            FOLLOWINGPOST fp2 = new FOLLOWINGPOST();
            //            fp2.ID = SetIDFollwingPost();
            //            fp2.ID_POST = likepost.ID_PC;
            //            fp2.ID_FOLLOWER = likepost.ID_LIKER;
            //            data.FOLLOWINGPOSTs.InsertOnSubmit(fp2);
            //        }
            //        var person = (from inf in data.POSTs where inf.ID_POST == likepost.ID_PC select inf).SingleOrDefault();
            //        if (person != null)
            //        {
            //            NOTIFICATION nc = new NOTIFICATION();
            //            nc.ID = SetIDNotifications();
            //            nc.ID_OBJECT = likepost.ID_PC;
            //            nc.ID_PERSON = person.ID_POSTER;
            //            nc.STYLE = "Post";
            //            data.NOTIFICATIONs.InsertOnSubmit(nc);                       
            //        }
            //        data.SubmitChanges();
            //        return true;
            //    }
            //    catch
            //    {
            //        return false;
            //    }
            //}

            //[WebMethod]
            //public bool AddLikeComment(LIKE_PC likepost)
            //{
            //    try
            //    {
            //        var data = new Data_ChiaSeThongTinDataContext();
            //        string fr = data.LIKE_PCs.Last().ID;
            //        fr = fr.Replace("LP", string.Empty);
            //        long NewID = 1 + long.Parse(fr);
            //        likepost.ID = "LP" + NewID.ToString("00000000");
            //        data.LIKE_PCs.InsertOnSubmit(likepost);                    
            //        var person = (from inf in data.COMMENTs where inf.ID_COMMENT == likepost.ID_PC select inf).SingleOrDefault();
            //        if (person != null)
            //        {
            //            NOTIFICATION nc = new NOTIFICATION();
            //            nc.ID = SetIDNotifications();
            //            nc.ID_OBJECT = likepost.ID_PC;
            //            nc.ID_PERSON = person.ID_COMMENTATOR;
            //            nc.STYLE = "Comment";
            //            data.NOTIFICATIONs.InsertOnSubmit(nc);
                        
            //        }
            //        data.SubmitChanges();
            //        return true;
            //    }
            //    catch
            //    {
            //        return false;
            //    }
            //}

            [WebMethod]
            public bool AddLike(LIKE_PC likepost)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    string fr = data.LIKE_PCs.Last().ID;
                    fr = fr.Replace("LP", string.Empty);
                    long NewID = 1 + long.Parse(fr);
                    likepost.ID = "LP" + NewID.ToString("00000000");
                    data.LIKE_PCs.InsertOnSubmit(likepost);
                    var person = (from inf in data.COMMENTs where inf.ID_COMMENT == likepost.ID_PC select inf).SingleOrDefault();
                    if (person != null)
                    {
                        NOTIFICATION nc = new NOTIFICATION();
                        nc.ID = SetIDNotifications();
                        nc.ID_OBJECT = likepost.ID_PC;
                        nc.ID_PERSON = person.ID_COMMENTATOR;
                        nc.STYLE = likepost.STYLE;
                        data.NOTIFICATIONs.InsertOnSubmit(nc);
                    }
                    if (likepost.STYLE == "Post")
                    {
                        var follwingpost = (from inf in data.FOLLOWINGPOSTs where inf.ID_POST == likepost.ID_PC select inf).ToList();
                        bool ok = true;
                        foreach (var fp in follwingpost)
                        {
                            if (fp.ID_FOLLOWER == likepost.ID_LIKER)
                            {
                                ok = false;
                                break;
                            }
                        }
                        if (ok)
                        {
                            FOLLOWINGPOST fp2 = new FOLLOWINGPOST();
                            fp2.ID = SetIDFollwingPost();
                            fp2.ID_POST = likepost.ID_PC;
                            fp2.ID_FOLLOWER = likepost.ID_LIKER;
                            data.FOLLOWINGPOSTs.InsertOnSubmit(fp2);
                        }
                    }
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }


            [WebMethod]
            public bool DeleteLike(string ID)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.LIKE_PCs where inf.ID == ID select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    data.LIKE_PCs.DeleteOnSubmit(temp);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }

            }

        #endregion

        #region Dislike
            [WebMethod]
            public DISLIKE_PC[] GetDislikePC(string ID_post)
            {
                var data = new Data_ChiaSeThongTinDataContext();
                var temp = (from inf in data.DISLIKE_PCs where inf.ID_PC == ID_post select inf).ToArray();
                return temp;  
            }

            //[WebMethod]
            //public bool AddDislikePost(DISLIKE_PC dislikepost)
            //{
            //    try
            //    {
            //        var data = new Data_ChiaSeThongTinDataContext();
            //        string fr = data.DISLIKE_PCs.Last().ID;
            //        fr = fr.Replace("DLP", string.Empty);
            //        long NewID = 1 + long.Parse(fr);
            //        dislikepost.ID = "DLP" + NewID.ToString("00000000");
            //        data.DISLIKE_PCs.InsertOnSubmit(dislikepost);
            //        var follwingpost = (from inf in data.FOLLOWINGPOSTs where inf.ID_POST == dislikepost.ID_PC select inf).ToList();
            //        bool ok = true;
            //        foreach (var fp in follwingpost)
            //        {
            //            if (fp.ID_FOLLOWER == dislikepost.ID_DISLIKER)
            //            {
            //                ok = false;
            //                break;
            //            }
            //        }
            //        if (ok)
            //        {
            //            FOLLOWINGPOST fp2 = new FOLLOWINGPOST();
            //            fp2.ID = SetIDFollwingPost();
            //            fp2.ID_POST = dislikepost.ID_PC;
            //            fp2.ID_FOLLOWER = dislikepost.ID_DISLIKER;
            //            data.FOLLOWINGPOSTs.InsertOnSubmit(fp2);
            //        }
            //        var person = (from inf in data.POSTs where inf.ID_POST == dislikepost.ID_PC select inf).SingleOrDefault();
            //        if (person != null)
            //        {
            //            NOTIFICATION nc = new NOTIFICATION();
            //            nc.ID = SetIDNotifications();
            //            nc.ID_OBJECT = dislikepost.ID_PC;
            //            nc.ID_PERSON = person.ID_POSTER;
            //            nc.STYLE = "Post";
            //            data.NOTIFICATIONs.InsertOnSubmit(nc);
            //            data.SubmitChanges();
            //        }
            //        data.SubmitChanges();
            //        return true;
            //    }
            //    catch
            //    {
            //        return false;
            //    }
            //}

            //[WebMethod]
            //public bool AddDislikeComment(DISLIKE_PC dislikepost)
            //{
            //    try
            //    {
            //        var data = new Data_ChiaSeThongTinDataContext();
            //        string fr = data.DISLIKE_PCs.Last().ID;
            //        fr = fr.Replace("DLP", string.Empty);
            //        long NewID = 1 + long.Parse(fr);
            //        dislikepost.ID = "DLP" + NewID.ToString("00000000");
            //        data.DISLIKE_PCs.InsertOnSubmit(dislikepost);
            //        var person = (from inf in data.COMMENTs where inf.ID_COMMENT == dislikepost.ID_PC select inf).SingleOrDefault();
            //        if (person != null)
            //        {
            //            NOTIFICATION nc = new NOTIFICATION();
            //            nc.ID = SetIDNotifications();
            //            nc.ID_OBJECT = dislikepost.ID_PC;
            //            nc.ID_PERSON = person.ID_COMMENTATOR;
            //            nc.STYLE = "Comment";
            //            data.NOTIFICATIONs.InsertOnSubmit(nc);
            //        }
            //        data.SubmitChanges();
            //        return true;
            //    }
            //    catch
            //    {
            //        return false;
            //    }
            //}
           
            [WebMethod]
            public bool AddDislike(DISLIKE_PC dislikepost)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    string fr = data.DISLIKE_PCs.Last().ID;
                    fr = fr.Replace("DLP", string.Empty);
                    long NewID = 1 + long.Parse(fr);
                    dislikepost.ID = "DLP" + NewID.ToString("00000000");
                    data.DISLIKE_PCs.InsertOnSubmit(dislikepost);                   
                    var person = (from inf in data.POSTs where inf.ID_POST == dislikepost.ID_PC select inf).SingleOrDefault();
                    if (person != null)
                    {
                        NOTIFICATION nc = new NOTIFICATION();
                        nc.ID = SetIDNotifications();
                        nc.ID_OBJECT = dislikepost.ID_PC;
                        nc.ID_PERSON = person.ID_POSTER;
                        nc.STYLE = dislikepost.STYLE;
                        data.NOTIFICATIONs.InsertOnSubmit(nc);
                        data.SubmitChanges();
                    }
                    if (dislikepost.STYLE == "Post")
                    {
                        var follwingpost = (from inf in data.FOLLOWINGPOSTs where inf.ID_POST == dislikepost.ID_PC select inf).ToList();
                        bool ok = true;
                        foreach (var fp in follwingpost)
                        {
                            if (fp.ID_FOLLOWER == dislikepost.ID_DISLIKER)
                            {
                                ok = false;
                                break;
                            }
                        }
                        if (ok)
                        {
                            FOLLOWINGPOST fp2 = new FOLLOWINGPOST();
                            fp2.ID = SetIDFollwingPost();
                            fp2.ID_POST = dislikepost.ID_PC;
                            fp2.ID_FOLLOWER = dislikepost.ID_DISLIKER;
                            data.FOLLOWINGPOSTs.InsertOnSubmit(fp2);
                        }
                    }
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }

            [WebMethod]
            public bool DeleteDislike(string ID)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.DISLIKE_PCs where inf.ID == ID select inf).FirstOrDefault();
                    if (temp == null)
                        return false;
                    data.DISLIKE_PCs.DeleteOnSubmit(temp);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }

            [WebMethod]
            public long GetNumberDislikePC(string ID_post)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.DISLIKE_PCs where inf.ID_PC == ID_post select inf);
                    if (temp == null)
                        return 0;
                    return temp.Count();
                }
                catch
                {
                    return 0;
                }
            }

            [WebMethod]
            public DISLIKE_PC[] GetMyDislikes(string ID_host)
            {
                var data = new Data_ChiaSeThongTinDataContext();
                var temp = (from inf in data.DISLIKE_PCs where inf.ID_DISLIKER == ID_host select inf).ToArray();
                return temp;
            }        

            [WebMethod]
            public decimal? GetNumberDislikes(string ID_host)
            {
                try
                {
                    var data = new Data_ChiaSeThongTinDataContext();
                    var temp = (from inf in data.DISLIKEs where inf.ID_HOST == ID_host select inf.NUMB).FirstOrDefault();
                    if (temp == null)
                        return 0;
                    return temp;
                }
                catch
                {
                    return 0;
                }
            }

        #endregion
        
        #region Chế độ xem
      
        /* 
         * 0 - only me
         * 1 - public
         * 2 - friend
         * 3 - someone
         * tạm thời chưa có chế độ 3.
         * tạo themem bảng lưu các đối tượng
        */
            [WebMethod]
            public CHEDOXEM[] GetAllCDX()
            {
                var data = new Data_ChiaSeThongTinDataContext();
                var temp = (from inf in data.CHEDOXEMs select inf).ToArray();
                return temp; 
            }
        #endregion

    
        #region sddf
        #endregion
        #region sddf
        #endregion
        #region sddf
        #endregion
        #region sddf
        #endregion
        #region sddf
        #endregion
        #region sddf
        #endregion
    }
    #region new data file
    [DataContract]
    public class NPost
    {
        [DataMember]
        public POST post;
        [DataMember]
        public decimal likes;
        [DataMember]
        public decimal dislikes;

        
    }

    [DataContract]
    public class NComment
    {
        [DataMember]
        public COMMENT comment;
        [DataMember]
        public decimal likes;
        [DataMember]
        public decimal dislikes;
        public NComment(COMMENT cm, decimal l, decimal dl)
        {
            comment = cm;
            likes = l;
            dislikes = dl;
        }
        public NComment()
        { }
    }
    #endregion
}